/**
 * Copyright &copy; 2012-2014 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.flashnews.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.modules.flashnews.entity.ExFlashNews;
import com.thinkgem.jeesite.modules.flashnews.service.ExFlashNewsService;

/**
 * 视屏新闻Controller
 * @author 张朋
 * @version 2016-05-19
 */
@Controller
@RequestMapping(value = "${adminPath}/flashnews/exFlashNews")
public class ExFlashNewsController extends BaseController {

	@Autowired
	private ExFlashNewsService exFlashNewsService;
	
	@ModelAttribute
	public ExFlashNews get(@RequestParam(required=false) String id) {
		ExFlashNews entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = exFlashNewsService.get(id);
		}
		if (entity == null){
			entity = new ExFlashNews();
		}
		return entity;
	}
	
	@RequiresPermissions("flashnews:exFlashNews:view")
	@RequestMapping(value = {"list", ""})
	public String list(ExFlashNews exFlashNews, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<ExFlashNews> page = exFlashNewsService.findPage(new Page<ExFlashNews>(request, response), exFlashNews); 
		model.addAttribute("page", page);
		return "modules/flashnews/exFlashNewsList";
	}

	@RequiresPermissions("flashnews:exFlashNews:view")
	@RequestMapping(value = "form")
	public String form(ExFlashNews exFlashNews, Model model) {
		model.addAttribute("exFlashNews", exFlashNews);
		return "modules/flashnews/exFlashNewsForm";
	}

	@RequiresPermissions("flashnews:exFlashNews:edit")
	@RequestMapping(value = "save")
	public String save(ExFlashNews exFlashNews, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, exFlashNews)){
			return form(exFlashNews, model);
		}
		exFlashNewsService.save(exFlashNews);
		addMessage(redirectAttributes, "保存视屏新闻成功");
		return "redirect:"+Global.getAdminPath()+"/flashnews/exFlashNews/?repage";
	}
	
	@RequiresPermissions("flashnews:exFlashNews:edit")
	@RequestMapping(value = "delete")
	public String delete(ExFlashNews exFlashNews, RedirectAttributes redirectAttributes) {
		exFlashNewsService.delete(exFlashNews);
		addMessage(redirectAttributes, "删除视屏新闻成功");
		return "redirect:"+Global.getAdminPath()+"/flashnews/exFlashNews/?repage";
	}

}